﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Framework.Library.Base.Base;
using Framework.Library.Base.Extensions;
using Framework.Library.Core.Utils;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAO;
using Framework.Library.Data.DAL.JObject;

namespace Framework.Portal.Layouts.Admin.Components.Users
{
    public partial class UserManagement : BaseControl
    {
        private readonly string MENUNAME = string.Empty;

        public UserManagement()
        {
        }

        public UserManagement(JSection section)
        {
            MENUNAME = section.title;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                var arrLetters = new ArrayList();
                for (char c = 'A'; c <= 'Z'; c++)
                {
                    arrLetters.Add(c.ToString());
                }
                arrLetters.Add("All");
                rptLetters.DataSource = arrLetters;
                rptLetters.DataBind();
                lblCommandName.Text = MENUNAME;
            }
        }

        protected void rptLetters_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var lnkLetter = (LinkButton) e.Item.FindControl("lnkLetter");
                lnkLetter.Text = e.Item.DataItem.ToString();
                lnkLetter.CommandArgument = e.Item.DataItem.ToString();
            }
        }

        protected void rptLetters_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Alpha")
            {
                if (ConvertUtility.ToString(e.CommandArgument) != "All")
                {
                    LoadUser(e.CommandArgument.ToString());
                }
                else
                {
                    LoadUser("");
                }
            }
        }

        protected void dtgUsers_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var btnImgDeleteUser = (ImageButton) e.Item.FindControl("btnImgDeleteUser");
                var lblKey = (HiddenField) e.Item.FindControl("lblKey");
                var lblUsername = (Label) e.Item.FindControl("lblUsername");
                var lblEmail = (Label) e.Item.FindControl("lblEmail");

                btnImgDeleteUser.OnClientClick = Localization.GetText("MESSAGE", "MSG_DELETE_CONFIRM");
                ;
                var curData = (PortalUser) e.Item.DataItem;

                if ((curData.ISSUPERADMIN > 0) || (lblEmail.Text == cfg.AdminMail.VALUE) ||
                    (lblUsername.Text == Session.GetUser().USERNAME))
                {
                    if (lblEmail.Text == cfg.AdminMail.VALUE)
                        e.Item.Visible = false;
                    btnImgDeleteUser.Visible = false;
                }
            }
        }

        protected void dtgUsers_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            int userId = ConvertUtility.ToInt32(e.CommandArgument);
            switch (e.CommandName)
            {
                case "manageprofile":
                    Response.Redirect("/Default.aspx?p=3&l=vi-vn&m=30&t=2&uid=" + userId);
                    break;
                case "managepassword":
                    Response.Redirect("/Default.aspx?p=3&l=vi-vn&m=30&t=3&uid=" + userId);
                    break;
                case "deleteuser":
                    try
                    {
                        ManagerFactory.DAOEntity.Delete(new PortalUser { USERID = userId });
                        lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                        LoadUser("");
                    }
                    catch (Exception ex)
                    {
                        lblUpdateStatus.Text = ex.Message;
                    }
                    break;
                default:
                    break;
            }
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Default.aspx?p=3&l=vi-vn&m=30&t=2");
        }

        private void LoadUser(string filter)
        {
            List<PortalUser> dtUsers = ManagerFactory.PortalUserManager.GetUsers("%" + filter + "%");
            dtgUsers.DataSource = dtUsers;
            dtgUsers.DataBind();
        }
    }
}